<?php
namespace app\admin\controller;
use app\admin\controller\Base;
class Scar extends Base
{
	function index(){
		if(\Request::isAjax()){
			$map = Input('get.');
			//P($map);
			$Scar = model('Scar');
			$map['limit'] = "all";
			$data = $Scar->getList($map);

			$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
			$sheet = $spreadsheet->getActiveSheet();
			$sheet->getPageSetup()->setOrientation(\PhpOffice\PhpSpreadsheet\Worksheet\PageSetup::ORIENTATION_LANDSCAPE);
			$sheet->mergeCells('B1:G1');
			$sheet->setCellValue('B1', '二手车');
			$sheet->setCellValue('H1', $map['date']);
			$sheet->setCellValue('A2', '序号');
			$sheet->setCellValue('B2', '买入日期');
			$sheet->setCellValue('C2', '概要');
			$sheet->setCellValue('D2', '买入金额');
			$sheet->setCellValue('E2', '其他费用');
			$sheet->setCellValue('F2', '卖出金额');
			$sheet->setCellValue('G2', '卖出日期');
			$sheet->setCellValue('H2', '利润');
			$i = 3;
			foreach ($data['list'] as $key => $value) {
				$sheet->setCellValue('A'.($i+$key), $key+1);
				$sheet->setCellValue('B'.($i+$key), $value['date']);
				$sheet->setCellValue('C'.($i+$key), $value['des']);
				$sheet->setCellValue('D'.($i+$key), $value['inprice']);
				$sheet->setCellValue('E'.($i+$key), $value['other']);
				$sheet->setCellValue('F'.($i+$key), $value['outprice']);
				$sheet->setCellValue('G'.($i+$key), $value['out_date']);	
				$sheet->setCellValue('H'.($i+$key), $value['outprice']-$value['inprice']-$value['other']);				
				//P($value);
			}
			$row = count($data['list'])+2;
			//$sheet->setCellValue('G'.($row+1),'=SUM(G3:G'.$row.')');

			$sheet->getColumnDimension('A')->setWidth(8);
			$sheet->getColumnDimension('B')->setWidth(13.29);
			$sheet->getColumnDimension('C')->setWidth(52);
			$sheet->getColumnDimension('D')->setWidth(10);
			$sheet->getColumnDimension('E')->setWidth(10);
			$sheet->getColumnDimension('F')->setWidth(10);
			$sheet->getColumnDimension('G')->setWidth(10);
			$sheet->getColumnDimension('H')->setWidth(14.29);
			$sheet->getDefaultRowDimension()->setRowHeight(20);
			$sheet->getRowDimension('1')->setRowHeight(40);

			$sheet->getStyle('A1:H'.$row)->applyFromArray([
			    'borders' => [
			        'allBorders' => [
			            'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
			            'color' => ['argb' => 'FF000000'],
			        ],
			    ],
			    'alignment' => [
			        'vertical' => \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER,
			    ],
			]);

			$sheet->getStyle('B1')->applyFromArray([
			    'alignment' => [
			        'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
			    ],
			]);

			$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
			$path = '/Uploads/monthtable/'.$map['out_date'].'.xlsx';
			$writer->save('.'.$path);
			$this->success('success',$path);
			//header('Location:'.$path);
		}else{
        	return view('index');
			//parent::index();
			//exit;
		}
	}
}
